package com.zdb.util;

import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MinioUtils {

    private static final Logger log = LoggerFactory.getLogger(MinioUtils.class);
    private final MinioClient minioClient;
    private final String bucketName;
    public MinioUtils(
            @Value("${minio-util.endpoint}") String endpoint,
            @Value("${minio-util.access-key}") String accessKey,
            @Value("${minio-util.secret-key}") String secretKey,
            @Value("${minio-util.bucket-name}") String bucketName
    ){
        this.minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
        this.bucketName = bucketName;
    }
    // 创建 Bucket
    public void makeBucket() throws Exception {
        boolean found = minioClient.bucketExists(
                BucketExistsArgs.builder().bucket(bucketName).build()
        );
        if (!found) {
            minioClient.makeBucket(
                    MakeBucketArgs.builder().bucket(bucketName).build()
            );
        }
    }

    // 上传文件
    public void uploadFile(String objectName, MultipartFile file) throws Exception {
        try (InputStream stream = file.getInputStream()) {  // 自动关闭流
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .stream(stream, file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build()
            );
        } catch (IOException e) {
           log.error("上传文件失败", e);
            throw e;
        }
    }

    // 下载文件
    public InputStream downloadFile(String objectName) throws Exception {
        return minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build()
        );
    }

    // 删除文件
    public void deleteFile(String objectName) throws Exception {
        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build()
        );
    }

    // 生成预签名 URL
    public String getPresignedUrl(String objectName, int expireSeconds) throws Exception {
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .method(Method.GET)
                        .expiry(expireSeconds, TimeUnit.SECONDS)
                        .build()
        );
    }

    // 获取所有对象
    public List<String> listObjects() throws Exception {
        Iterable<Result<Item>> items = minioClient.listObjects(
                ListObjectsArgs.builder().bucket(bucketName).build()
        );
        List<String> objectNames = new ArrayList<>();
        for (Result<Item> item : items) {
            objectNames.add(item.get().objectName());
        }
        return objectNames;
    }

}
